import { defineStore } from "pinia";
import router from "@/router";
import request from "@/plugins/request";

export const useHeaderStore = defineStore('HeaderStore', () => {
    const userInfo = reactive(localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo') as string) : {}) 
    const item_function = reactive([
        { id: 5, name: '搜索', url: '/search' },
        { id: 1, name: '分类', url: '/cetegora' },
        { id: 2, name: '排行', url: '/top' },
        { id: 3, name: '上传', url: '/upload' },
        { id: 4, name: '历史', url: '/history' }
    ])

    // 方法
    const handleCommand = (commond: string | number | object) => {
        // 路由跳转
        router.push({ path: commond as string })
    }
    async function exitLogin(){
        // 发送请求退出登录
        const {data} = await request.post('/userInfo/exitLogin')
        console.log(data)
        // localStorage.removeItem('userInfo')
    }

    return {
        userInfo,
        item_function,
        handleCommand,
        exitLogin,
    }
})